// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;

namespace Matrix.ServiceCommunication.Options;

/// <summary>
/// 服务间通信配置选项
/// </summary>
public class ServiceCommunicationOptions
{
    /// <summary>
    /// 配置节名称
    /// </summary>
    public const string SectionName = "ServiceCommunication";

    /// <summary>
    /// 默认超时时间（秒）
    /// </summary>
    public int DefaultTimeoutSeconds { get; set; } = 30;

    /// <summary>
    /// 默认重试次数
    /// </summary>
    public int DefaultRetryCount { get; set; } = 3;

    /// <summary>
    /// 启用断路器
    /// </summary>
    public bool EnableCircuitBreaker { get; set; } = true;

    /// <summary>
    /// 断路器失败阈值
    /// </summary>
    public int CircuitBreakerFailureThreshold { get; set; } = 5;

    /// <summary>
    /// 断路器恢复时间（秒）
    /// </summary>
    public int CircuitBreakerRecoverySeconds { get; set; } = 30;

    /// <summary>
    /// 服务发现配置
    /// </summary>
    public ServiceDiscoveryOptions ServiceDiscovery { get; set; } = new();

    /// <summary>
    /// 认证配置
    /// </summary>
    public AuthenticationOptions Authentication { get; set; } = new();

    /// <summary>
    /// 日志配置
    /// </summary>
    public LoggingOptions Logging { get; set; } = new();

    /// <summary>
    /// 容错配置
    /// </summary>
    public ResilienceOptions Resilience { get; set; } = new();

    /// <summary>
    /// 服务发现增强配置
    /// </summary>
    public EnhancedDiscoveryOptions EnhancedDiscovery { get; set; } = new();

    /// <summary>
    /// 监控配置
    /// </summary>
    public MonitoringOptions Monitoring { get; set; } = new();

    /// <summary>
    /// 健康检查配置
    /// </summary>
    public HealthCheckOptions HealthCheck { get; set; } = new();
}

/// <summary>
/// 服务发现配置
/// </summary>
public class ServiceDiscoveryOptions
{
    /// <summary>
    /// 启用服务发现
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 服务注册中心地址
    /// </summary>
    public string? ServiceRegistryUrl { get; set; }

    /// <summary>
    /// 服务实例缓存时间（秒）
    /// </summary>
    public int CacheSeconds { get; set; } = 60;
}

/// <summary>
/// 认证配置
/// </summary>
public class AuthenticationOptions
{
    /// <summary>
    /// 认证类型
    /// </summary>
    public AuthenticationType Type { get; set; } = AuthenticationType.ClientCredentials;

    /// <summary>
    /// 客户端ID
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// 客户端密钥
    /// </summary>
    public string? ClientSecret { get; set; }

    /// <summary>
    /// Token获取地址
    /// </summary>
    public string? TokenEndpoint { get; set; }

    /// <summary>
    /// Token缓存时间（秒）
    /// </summary>
    public int TokenCacheSeconds { get; set; } = 300;

    /// <summary>
    /// JWT发行者
    /// </summary>
    public string? Issuer { get; set; }

    /// <summary>
    /// JWT受众
    /// </summary>
    public string? Audience { get; set; }

    /// <summary>
    /// JWT密钥（用于对称加密签名验证）
    /// </summary>
    public string? SecretKey { get; set; }

    /// <summary>
    /// 时钟偏移（秒）
    /// </summary>
    public int ClockSkewSeconds { get; set; } = 300;

    /// <summary>
    /// JWT权限范围
    /// </summary>
    public string[]? Scopes { get; set; }
}

/// <summary>
/// 认证类型枚举
/// </summary>
public enum AuthenticationType
{
    /// <summary>
    /// 无认证
    /// </summary>
    None = 0,

    /// <summary>
    /// 客户端凭证模式
    /// </summary>
    ClientCredentials = 1,

    /// <summary>
    /// API密钥认证
    /// </summary>
    ApiKey = 2,

    /// <summary>
    /// JWT Bearer认证
    /// </summary>
    JwtBearer = 3,

    /// <summary>
    /// 混合认证（支持多种认证类型）
    /// </summary>
    Hybrid = 4
}

/// <summary>
/// 日志配置
/// </summary>
public class LoggingOptions
{
    /// <summary>
    /// 启用请求日志
    /// </summary>
    public bool EnableRequestLogging { get; set; } = true;

    /// <summary>
    /// 启用响应日志
    /// </summary>
    public bool EnableResponseLogging { get; set; } = true;

    /// <summary>
    /// 记录请求体
    /// </summary>
    public bool LogRequestBody { get; set; } = false;

    /// <summary>
    /// 记录响应体
    /// </summary>
    public bool LogResponseBody { get; set; } = false;

    /// <summary>
    /// 最大日志体大小（字节）
    /// </summary>
    public int MaxLogBodySize { get; set; } = 1024 * 4; // 4KB
}

/// <summary>
/// 容错配置选项
/// </summary>
public class ResilienceOptions
{
    /// <summary>
    /// 重试配置
    /// </summary>
    public RetryOptions Retry { get; set; } = new();

    /// <summary>
    /// 断路器配置
    /// </summary>
    public CircuitBreakerOptions CircuitBreaker { get; set; } = new();

    /// <summary>
    /// 超时配置
    /// </summary>
    public TimeoutOptions Timeout { get; set; } = new();
}

/// <summary>
/// 重试选项
/// </summary>
public class RetryOptions
{
    /// <summary>
    /// 最大重试次数
    /// </summary>
    public int MaxAttempts { get; set; } = 3;

    /// <summary>
    /// 延迟毫秒数
    /// </summary>
    public int DelayMilliseconds { get; set; } = 1000;

    /// <summary>
    /// 退避类型
    /// </summary>
    public BackoffType BackoffType { get; set; } = BackoffType.Exponential;
}

/// <summary>
/// 退避类型
/// </summary>
public enum BackoffType
{
    /// <summary>
    /// 固定延迟
    /// </summary>
    Constant,

    /// <summary>
    /// 线性增长
    /// </summary>
    Linear,

    /// <summary>
    /// 指数退避
    /// </summary>
    Exponential
}

/// <summary>
/// 断路器选项
/// </summary>
public class CircuitBreakerOptions
{
    /// <summary>
    /// 失败比例阈值
    /// </summary>
    public double FailureRatio { get; set; } = 0.3;

    /// <summary>
    /// 采样时间（秒）
    /// </summary>
    public int SamplingDurationSeconds { get; set; } = 60;

    /// <summary>
    /// 最小吞吐量
    /// </summary>
    public int MinimumThroughput { get; set; } = 10;

    /// <summary>
    /// 断路时间（秒）
    /// </summary>
    public int BreakDurationSeconds { get; set; } = 30;
}

/// <summary>
/// 超时选项
/// </summary>
public class TimeoutOptions
{
    /// <summary>
    /// 超时时间（秒）
    /// </summary>
    public int TimeoutSeconds { get; set; } = 25;
}

/// <summary>
/// 增强服务发现选项
/// </summary>
public class EnhancedDiscoveryOptions
{
    /// <summary>
    /// 启用增强服务发现
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 负载均衡策略
    /// </summary>
    public LoadBalancingStrategy LoadBalancingStrategy { get; set; } = LoadBalancingStrategy.RoundRobin;

    /// <summary>
    /// 健康检查间隔（秒）
    /// </summary>
    public int HealthCheckIntervalSeconds { get; set; } = 30;

    /// <summary>
    /// 服务注册TTL（秒）
    /// </summary>
    public int ServiceRegistrationTtlSeconds { get; set; } = 300;
}

/// <summary>
/// 负载均衡策略
/// </summary>
public enum LoadBalancingStrategy
{
    /// <summary>
    /// 轮询
    /// </summary>
    RoundRobin,

    /// <summary>
    /// 随机
    /// </summary>
    Random,

    /// <summary>
    /// 加权轮询
    /// </summary>
    WeightedRoundRobin,

    /// <summary>
    /// 最少连接
    /// </summary>
    LeastConnections
}

/// <summary>
/// 监控选项
/// </summary>
public class MonitoringOptions
{
    /// <summary>
    /// 启用链路追踪
    /// </summary>
    public bool EnableTracing { get; set; } = true;

    /// <summary>
    /// 启用指标收集
    /// </summary>
    public bool EnableMetrics { get; set; } = true;

    /// <summary>
    /// OpenTelemetry导出端点
    /// </summary>
    public string? OtlpEndpoint { get; set; }

    /// <summary>
    /// 服务名称
    /// </summary>
    public string ServiceName { get; set; } = "Matrix.ServiceCommunication";
}

/// <summary>
/// 健康检查选项
/// </summary>
public class HealthCheckOptions
{
    /// <summary>
    /// 启用健康检查
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 检查间隔（秒）
    /// </summary>
    public int IntervalSeconds { get; set; } = 30;

    /// <summary>
    /// 超时时间（秒）
    /// </summary>
    public int TimeoutSeconds { get; set; } = 5;

    /// <summary>
    /// 失败阈值
    /// </summary>
    public int FailureThreshold { get; set; } = 3;
}